#include <REGX52.H>

unsigned char KeyNum;

//----------------------------------
//对外获取按键值函数
unsigned char GetKey_Num(){
	unsigned char t;
	t = KeyNum;   //返回值
	KeyNum = 0;   //每次清零
	return t;
}


unsigned char GetKeyNum()//获取键码函数(内部调用)
{
	unsigned char Key=0;
	if(P3_1==0) Key=1;//按下Key代表键码,松手是Key = 0
	if(P3_0==0) Key=2;
	if(P3_2==0) Key=3;
	if(P3_3==0) Key=4;

	return Key;//返回键码数值
}


void KeyScan(){
	static unsigned char nowKey,lastKey; 
	lastKey = nowKey;
	
	
	nowKey = GetKeyNum();//获取按键值
	
	//按下&&抬起 有时间差10ms扫描，直到松手
	if(lastKey == 1 && nowKey == 0){KeyNum = 1;}
	if(lastKey == 2 && nowKey == 0){KeyNum = 2;}
	if(lastKey == 3 && nowKey == 0){KeyNum = 3;}
	if(lastKey == 4 && nowKey == 0){KeyNum = 4;}
}